package com.jin.webvideo.externalInterfaces.spi.recommand;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jin.webvideo.externalInterfaces.iqiyi.Result;
import com.jin.webvideo.base.spi.Interface;
import com.jin.webvideo.base.spi.InterfaceProvider;
import com.jin.webvideo.base.spi.anno.Register;
import com.jin.webvideo.base.spi.exception.InterfaceRunningException;
import com.jin.webvideo.util.sys.ClientUtil;
import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.util.base.URLUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Register(name = "iqiyi")
public class IQIYIRecommendInterface extends Interface {
    //%ID%=90927301;
    private static final String URL = "https://suggest.video.iqiyi.com/?rltnum=%count%&key=%key%";
    private static final String HOT_SEARCH = "https://search.video.iqiyi.com/m?if=hotQueryNew&platform=14&pagesize=%count%&hot_query_type=1";

    public IQIYIRecommendInterface(InterfaceProvider provider) {
        super(provider);
    }

    public List<String> getResult(String key, String count) throws IOException {
        if (key.isEmpty()){
            return hot(count);
        }
        return recommend(key, count);
    }

    public List<String> recommend(String key, String count) throws IOException {
        List<String> recommend = new ArrayList<>();

        String json = ClientUtil.httpGet(URLUtil.format(URL, count, key));
        Result result = JSONObject.parseObject(json, Result.class);
        if (!StringUtil.isEmpty(result.getData())){
            JSONArray array = JSONObject.parseArray(result.getData());
            for (int i = 0, l = array.size(); i < l; i ++){
                JSONObject jsonObj = array.getJSONObject(i);
                String name = jsonObj.getString("name");
                if (!StringUtil.isEmpty(name)){
                    recommend.add(name);
                }
            }
        }
        return recommend;
    }
    public List<String> hot(String count) throws IOException {
        List<String> recommend = new ArrayList<>();

        String json = ClientUtil.httpGet(URLUtil.format(HOT_SEARCH, count));
        JSONObject result = JSONObject.parseObject(json);
        JSONArray hotQuery = result.getJSONArray("hot_query");
        result = hotQuery.getJSONObject(0);
        hotQuery = result.getJSONArray("hot_query_info");
        for (int i = 0, l = hotQuery.size(); i < l; i ++){
            JSONObject hot = hotQuery.getJSONObject(i);
            String name = hot.getString("query");
            if (!StringUtil.isEmpty(name)){
                recommend.add(name);
            }
        }
        return recommend;
    }

    @Override
    public Object run(Object... param) throws InterfaceRunningException {
        try {
            return getResult(param[0].toString(), param[1].toString());
        } catch (IOException e) {
            throw new InterfaceRunningException(e.getCause());
        }
    }
}
